#ifndef __CONNECTIONHANDLER_HPP__
#define __CONNECTIONHANDLER_HPP__

#include <sstream>

#include "TCPConnection.hpp"
#include "GenericException.hpp"

/**
 * Abstract base class of anything that can handle a new TCP connection
 */
class ConnectionHandler {

	protected:
		TCPConnection* connection; /**< underlying TCPConnection */

	public:
		ConnectionHandler() {
			connection = NULL;
		}

		ConnectionHandler(TCPConnection& connection) : connection(&connection) {
		}

		void setConnection(TCPConnection* connection) {
			this->connection = connection;
		}

		virtual void handle() = 0;

		void handle(TCPConnection* connection) {
			setConnection(connection);
			try {
				handle();
			} catch (const GenericException& e) {
				std::cerr << "Child connection to " << connection->getRemoteStr() << " died of uncaught exception: " << e;
			}
		}

		virtual ~ConnectionHandler() {
			delete connection;
		}

};
#endif

